package util;

import com.demo.entity.Goods;
import com.demo.service.OrgHolder;
import com.demo.service.ParamHolder;
import com.demo.util.ThreadPoolUtil;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Semaphore;

public class ThreadDemo {
    private Logger logger = LoggerFactory.getLogger(getClass());
    ThreadLocal<String> td = new ThreadLocal<>();

    @Test
    public void semaphoreTest() {
        int machineNum = 2;
        int workerNum = 5;
        Semaphore semaphore = new Semaphore(machineNum);
        for (int i = 1; i <= workerNum; i++) {
            new Worker(i, semaphore).start();
        }
        try {
            Thread.currentThread().join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void t2() {
        int machineNum = 1;
        int workerNum = 5;
        Semaphore semaphore = new Semaphore(machineNum);
        try {
            semaphore.acquire();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (int i = 1; i <= workerNum; i++) {
            new Worker(i, semaphore).start();
        }
        semaphore.release();
        try {
            Thread.currentThread().join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    private static class Worker extends Thread {
        private int workerId;
        private Semaphore semaphore;
        private Logger logger = LoggerFactory.getLogger(getClass());

        public Worker(int workerId, Semaphore semaphore) {
            this.workerId = workerId;
            this.semaphore = semaphore;
        }

        @Override
        public void run() {
            try {
                logger.debug("工人 {} 尝试获取机器..", workerId);
                logger.debug("当前可用机器：{}", semaphore.availablePermits());
                semaphore.acquire();
                logger.debug("工人 {} 获得机器，开始操作..", workerId);
                Thread.sleep(2000);
                logger.debug("工人 {} 完成工作，释放机器..", workerId);
                semaphore.release();
                logger.debug("当前可用机器：{}", semaphore.availablePermits());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    @Test
    public void t3() {
        ThreadPoolUtil.execute(() -> {
            logger.debug("执行任务...");
        });
        try {
            Thread.currentThread().join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void t4() {
        String ret = "";
        try {
            ret = ThreadPoolUtil.submit(() -> {
                String str = "线程名:" + Thread.currentThread().getName();
                Thread.sleep(2000);
                if (str.length() > 0) {
                    throw new IllegalStateException("异常状态");
                }
                return str;
            }).get();
        } catch (InterruptedException | ExecutionException e) {
//            e.printStackTrace();
            logger.error("异常了", e);
        }
        logger.debug(ret);

    }

    @Test
    public void t5() {
        ParamHolder.set("ABC");
        OrgHolder.set("ORG");
        String s = ParamHolder.get();
        ParamHolder.remove();
        s = ParamHolder.get();
    }

    @Test
    public void t6() {
        List<Goods> list = new ArrayList<>();
        for (long i = 0; i < 100000000000000L; i++) {
            Goods goods = new Goods();
            goods.setGoodsId("i");
            list.add(goods);
        }
        System.out.println(1);
        Goods g1,g2 = new Goods();
    }

}

















